﻿(function ($) {
    var methods = {
        init: function (opts) {
            if (!$(window).data('register_messageline')) {
                $(window).data('register_messageline', true)
            } else {
                alert('error:只允许有一个请求总线')
                return;
            }
            var defaults = {
                actions: [],
                url: "blank.html",
                timeout: 90000//默认ajax请求超时时间80秒  
            }
            var setting = $.extend(true, defaults, opts)
            if (!$(window).data('messageline')) {
                $(window).data('messageline', [])
            }
            $.each(setting.actions, function (i, item) {
                $(window).data('messageline').push(item)
            })
            function messageline() {
                $.ajax({
                    type: "GET",
                    dataType: "json",
                    url: setting.url,
                    timeout: setting.timeout,
                    data: setting.data,
                    success: function (messages, textStatus) {
                        //从服务器得到数据，显示数据并继续查询  
                        $.each(messages, function (i, item) {
                            $.each($(window).data('messageline'), function (j, active) {
                                if (active.name === item.name) {
                                    active.action(item.data)
                                }
                            })
                        })
                        messageline();
                    },
                    //Ajax请求超时，继续查询  
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (textStatus == "timeout") {
                            messageline()
                        }
                    }
                });
            }
            messageline();
        },
        addlistener: function (active) {
            if (!$(window).data('messageline')) {
                $(window).data('messageline', [])
            }
            $(window).data('messageline').push(active)
        }
    };
    $.comet = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else
            if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            }
            else {
                $.error('Method ' + method + ' does not exist on jQuery.tooltip');
            }
    };
})(jQuery);

//初始化（仅需要一次）
$.comet({
    url: 'WebForm1.aspx'
})

var i = 1;
function _refresh() {
    $.get("WebForm1.aspx").done(function (data) {
        //alert(data);
        $("#username").val(i + data);
        $("#mRefresh")[0].src = "WebForm1.aspx?r=" + Math.random();
        setTimeout(_refresh, 2 * 60000);
    });
}
setTimeout(_refresh, 2 * 60000);